#include "PrecompiledHeader.h"
#include "OrthoCamera.h"

/*--------------------------------------------------------------------------*/

OrthoCamera::OrthoCamera()
{
	this->Update();
}

/*--------------------------------------------------------------------------*/

OrthoCamera::~OrthoCamera()
{

}

/*--------------------------------------------------------------------------*/

bool OrthoCamera::Update()
{
	if (IsOn(this->m_updateFlags, ECameraUpdateFlag::ViewportChanged))
	{
		bool updatedSth = this->UpdateViewMatrix();
		updatedSth |= this->UpdateProjectionMatrix();

		if (updatedSth)
		{
			this->m_viewProjectionMatrix = this->m_projectionMatrix * this->m_viewMatrix;
		}

		SetOff(this->m_updateFlags, ECameraUpdateFlag::ViewportChanged);

		return true;
	}

	return false;
}

/*--------------------------------------------------------------------------*/

bool OrthoCamera::IsVisible(const glm::vec3& space, const float& radius) const
{
	return true;
}

/*--------------------------------------------------------------------------*/

glm::vec2 OrthoCamera::GetOnScreenCoords(const glm::mat4& modelmatrix, const glm::vec3& pnt, bool* visible) const
{
	return glm::vec2(pnt.x, pnt.y);
}

/*--------------------------------------------------------------------------*/

bool OrthoCamera::UpdateProjectionMatrix()
{
	if (IsOn(this->m_updateFlags, ECameraUpdateFlag::ProjectionMatrixChanged) || IsOn(this->m_updateFlags, ECameraUpdateFlag::ViewportChanged))
	{
		this->m_projectionMatrix = glm::ortho(this->m_viewport.x,
		                                      this->m_viewport.z,
		                                      this->m_viewport.y,
		                                      this->m_viewport.w,
		                                      -1.0f,
		                                      1.0f);

		SetOff(this->m_updateFlags, ECameraUpdateFlag::ProjectionMatrixChanged);
		SetOff(this->m_updateFlags, ECameraUpdateFlag::ViewportChanged);

		return true;
	}

	return false;
}

/*--------------------------------------------------------------------------*/

bool OrthoCamera::UpdateViewMatrix()
{
	if (IsOn(this->m_updateFlags, ECameraUpdateFlag::ViewMatrixChanged))
	{
		this->m_viewMatrix =  glm::translate(-this->m_position);
		this->m_invViewMatrix = glm::inverse(this->m_viewMatrix);

		this->m_strafedirection = glm::vec3(1.0f, 0.0f, 0.0f);
		this->m_jumpdirection = glm::vec3(0.0f, 1.0f, 0.0f);
		this->m_viewdirection = glm::vec3(0.0f, 1.0f, 1.0f);

		SetOff(this->m_updateFlags, ECameraUpdateFlag::ViewMatrixChanged);

		return true;
	}

	return false;
}

/*--------------------------------------------------------------------------*/